home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / borland / jnfb88.zip / SKYDIV.ZIP / RAPHSON2.PAS < prev   
Pascal/Delphi Source File  |  1987-10-22  |  3KB  |  71 lines

  1. PROGRAM Raphson2;
  2.  
  3. {--------------------------------------------------------------------}
  4. {                                                                   -}
  5. {  Turbo Pascal Numerical Methods Toolbox                           -}
  6. {-     (C) Copyright 1986 Borland International.                    -}
  7. {-                                                                  -}
  8. {-     Purpose: This sample program demonstrates the                -}
  9. {-              Newton-Raphson algorithm.  This program is very     -}
  10. {-              bare-boned; it contains no I/O checking.            -}
  11. {-                                                                  -}
  12. {-  Include Files: RAPHSON.INC         procedure Newton_Raphson     -}
  13. {-                                                                  -}
  14. {-       Version Date: 26 January 1987                              -}
  15. {-                                                                  -}
  16. {--------------------------------------------------------------------}
  17.  
  18. VAR
  19.   InitGuess : Real;           { Initial approximation }
  20.   Tolerance : Real;           { Tolerance in answer }
  21.   Root, Value, Deriv : Real;  { Resulting roots and other info }
  22.   Iter : Integer;             { Number of iterations to find root }
  23.   MaxIter : Integer;          { Maximum number of iterations }
  24.   Error : Byte;               { Error flag }
  25.   OutFile : Text;             { Output file }
  26.  
  27.  
  28.   {------- HERE IS THE FUNCTION ----------}
  29.   FUNCTION TNTargetF(X : Real) : Real;
  30.   BEGIN
  31.     TNTargetF := -40.0+X*(1.0-Exp(-98.1/X))/(1.0+Exp(-98.1/X));
  32.   END;                        { function TNTargetF }
  33.   {---------------------------------------}
  34.  
  35.   {------- HERE IS THE DERIVATIVE --------}
  36.   FUNCTION TNDerivF(X : Real) : Real;
  37.   VAR EE : Real;
  38.   BEGIN
  39.     EE := Exp(-98.1/X);
  40.     TNDerivF := (1.0-EE*(1.0+98.1/X))/(EE+1.0)+
  41.                  98.1*EE*(EE-1.0)/(X*Sqr(EE+1.0));
  42.   END;                        { function TNDerivF }
  43.   {---------------------------------------}
  44.  
  45.   {$I RAPHSON.INC}            { Load procedure Raphson }
  46.  
  47. BEGIN                         { program Newton_Raphson }
  48.   Write('Initial Approximation to the root: ');
  49.   ReadLn(InitGuess);
  50.   WriteLn;
  51.   Write('Tolerance (> 0, suggested 1E-6): ');
  52.   ReadLn(Tolerance);
  53.   WriteLn;
  54.   Write('Maximum number of iterations (>= 0, suggested 100): ');
  55.   ReadLn(MaxIter);
  56.   WriteLn;
  57.  
  58.   Newton_Raphson(InitGuess, Tolerance, MaxIter,
  59.                  Root, Value, Deriv, Iter, Error);
  60.  
  61.   WriteLn;
  62.   WriteLn('Error = ', Error);
  63.   WriteLn('Number of iterations: ':30, Iter:3);
  64.   WriteLn('Calculated root: ':30, Root);
  65.   WriteLn('Value of the function':28);
  66.   WriteLn('at the root: ':30, Value);
  67.   WriteLn('Value of the derivative':28);
  68.   WriteLn('of the function at the':28);
  69.   WriteLn('calculated root: ':30, Deriv);
  70. END.                          { program Newton_Raphson }
  71.